<template>
  <div class="city-list" ref="wrapper">
    <div>
      <!-- 当前城市开始 -->
      <div class="current-city-wrapper">
        <h3 class="current-title border-bottom">当前城市</h3>
        <div class="current-city-content">
          <div class="current-city">
            <div class="current-city-button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <!-- 当前城市结束 -->
      <!-- 热门城市开始 -->
      <div class="hot-city-wrapper">
        <h3 class="hot-city-title">热门城市</h3>
        <div class="hot-city-content">
          <div class="hot-city"
            v-for="item of hotCities"
            :key="item.id">
            <div class="hot-city-button"
              @click="chooseCity(item.name)">
              {{item.name}}</div>
          </div>
        </div>
      </div>
      <!-- 热门城市结束 -->
      <!-- 按照字母排序的城市列表开始 -->
      <div class="cities-wrapper">
        <ul class="cities-section"
          v-for="(item,index) of cities"
          :key="index">
          <li class="cities-item-title" :ref="index">{{index}}</li>
          <li class="city border-bottom"
            v-for="subItem of item"
            :key="subItem.id"
            @click="chooseCity(subItem.name)">{{subItem.name}}</li>
        </ul>
      </div>
      <!-- 按照字母排序的城市列表开始 -->
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'cityList',
  props: {
    info: Object,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  watch: {
    info (value) {
      this.getInfo()
    },
    letter () { // 滑到某个字母所在位置
      this.scroll.scrollToElement(this.$refs[this.letter][0])
    }
  },
  data () {
    return {
      hotCities: [],
      cities: {}
    }
  },
  mounted () { // 生成滚动条实例
    this.$nextTick(() => {
      this.scroll = new BScroll(this.$refs.wrapper, { mouseWheel: true, click: true, tap: true })
    })
  },
  methods: {
    getInfo () { // 获取数据
      this.hotCities = this.info.hotCities
      this.cities = this.info.cities
    },
    chooseCity (value) { // 选择城市，并且跳回到首页
      this.changeCity(value)
      this.$router.push('/home')
    },
    ...mapMutations({
      changeCity: 'changeCity'
    })
  }
}
</script>
<style lang="stylus" scoped>
@import '~@/assets/styles/mixins'
.city-list
  width 100%
  position absolute
  margin-top 100px
  left 0
  top 0
  font-size 20px
  overflow hidden
  height 100%
  // 当前城市
  .current-city-wrapper
    .current-title
      padding-left 10px
      background #eee
      line-height 1.5
    .current-city-content
      .current-city
        width 33.33%
        padding 10px 28px
        text-align center
        .current-city-button
          line-height 2
          border 1px solid grey
          border-radius 5px
          ellipsis()
  // 热门城市
  .hot-city-wrapper
    .hot-city-title
      padding-left 20px
      background #eee
      line-height 1.5
    .hot-city-content
      &:after
        display block
        content ' '
        clear both
      .hot-city
        width 33.33%
        float left
        padding 10px 28px
        text-align center
        .hot-city-button
          // border-radius 10px
          border 1px solid grey
          border-radius 5px
          line-height 2
          ellipsis()
  .cities-wrapper
    .cities-section
      .cities-item-title
        padding-left 10px
        background #eee
        line-height 1.5
      .city
        line-height 2
        padding-left 20px
</style>
